<?php
/**
 * Copyright 2011  SURFfoundation
 * 
 * This file is part of ESCAPE.
 * 
 * ESCAPE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ESCAPE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ESCAPE.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * For more information:
 * http://escapesurf.wordpress.com/
 * http://www.surffoundation.nl/
 * 
 * Contact: d.vierkant@utwente.nl
 * 
 * @license http://www.gnu.org/licenses/gpl.html GNU GPLv3
 */
?>
<?php
/**
 * Add ORE aggregation editor
 * 
 * Add an editor to an aggregation.
 */
class escape_repositoryui_rpc_AddOreAggregationEditor extends escape_repositoryui_RpcHandler
{
	public $contentType = 'text/html';
	
	public function handleRequest()
	{
		$containerElementId = $_REQUEST['containerElementId'];
		$aggregationUri = $_REQUEST['aggregationUri'];
		$editorUri = $_REQUEST['editorUri'];
		
		// fetch the aggregation
		$aggregation = $this->repository->getOreAggregationByUri($aggregationUri);
		if($aggregation === null)
		{
			throw new escape_repositoryui_exception_Exception('Failed to fetch the specified aggregation');
		}
				
		// only administrators and aggregation owners are allowed to add aggregation editors
		$user = $this->repositoryUi->user;
		if($user === null || !($user->hasSystemRole(escape_repository_User::$role_administrator) || $aggregation->hasOwner($user->getUri())))
		{
			throw new escape_repositoryui_exception_Rpc("Not authorized");
		}

		// add the new aggregation owner
		$aggregation->addEditor($editorUri);

		// display an editor fragment to select aggregation editors and owners
		$this->repositoryUi->displayOreAggregationEditorListEditor($aggregation, $containerElementId);
	}
}
